<?php
/*
金蝶接口
自定义引入类需要在composer.json 中定义和引入 然后再更新 composer dump-autoload
*/

namespace extend\services\nailin;

use Illuminate\Support\Facades\Cache;

/**
 * 金蝶相关服务
 * Class Schedul
 * @package services\designPartment
 */
class NewdemandService
{
    //请求的登录连接

    public $cloudUrl;

    private $kingDeeCookiePathBySession = 'KINGDEE_COOKIE_PATH';

    //登陆参数 61aedb931d9c97 正式账套
    //测试
    //正式 63a2e08d75cab3
    protected $Basdata;

    public function __construct()
    {
        $this->cloudUrl = env('KINGDEE_API_URL');
        $this->Basdata = [
            env('KINGDEE_API_DB'),
            env('KINGDEE_API_USER'),
            env('KINGDEE_API_PWD'),
            2052
        ];
    }

    //请求的登录连接
    public function KingdeeLogin()
    {

        // //有cookie临时文件不用登陆
        if(Cache::has($this->kingDeeCookiePathBySession)) return Cache::get($this->kingDeeCookiePathBySession);

        @$cookie_jar = tempnam('./tmp', 'CloudSession');

        //必须登录
        $post_content = $this->create_postdata($this->Basdata);
        //登录接口
        $result = $this->invoke_login($this->cloudUrl, $post_content,$cookie_jar);

        if(empty($result)){
            throw new \Exception('金蝶登陆接口返回空');
        }

        $result = json_decode($result,true);
        if(!isset($result['LoginResultType']) || $result['LoginResultType'] != 1){
            throw new \Exception('金蝶接口登陆失败：' . @$result['Message']);
        }

        //登陆状态缓存1小时
        Cache::put($this->kingDeeCookiePathBySession,$cookie_jar,3600);
        return $cookie_jar;

    }





    //构造Web API请求格式
    public function create_postdata($args)
    {
        $postdata = ['format' => 1, 'useragent' => 'ApiClient', 'rid' => self::create_guid(), 'parameters' => $args, 'timestamp' => date('Y-m-d'), 'v' => '1.0'];
        return json_encode($postdata);
    }

    //登陆
    public function invoke_login($cloudUrl, $post_content, $cookie_jar)
    {
        $loginurl = $cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc';
        return $this->invoke_post($loginurl, $post_content, $cookie_jar, TRUE);
    }

    //查看接口
    public function Toview($cloudUrl, $post_content, $cookie_jar)
    {
        $invokeurl = $cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc';
        return $this->invoke_post($invokeurl, $post_content, $cookie_jar, FALSE);
    }

    //单据查询接口
    public function ExecuteBillQuery($cloudUrl, $post_content)
    {
        $invokeurl = $cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc';
        return $this->invoke_post($invokeurl, $post_content, $this->KingdeeLogin(), FALSE);

    }
    //查询单据信息接口

    // public function QueryBusinessInfo($cloudUrl, $post_content, $cookie_jar)
    // {
    //     $invokeurl = $cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.QueryBusinessInfo.common.kdsvc';
    //     return $this->invoke_post($invokeurl, $post_content, $cookie_jar, FALSE);
    // }

    //查询单据信息接口
    public function QueryBusinessInfo($cloudUrl, $post_content, $cookie_jar)
    {
        $invokeurl = $cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.QueryBusinessInfo.common.kdsvc';
        return $this->invoke_post($invokeurl, $post_content, $cookie_jar, FALSE);
    }

    //查询分组信息接口
    public function QueryGroupInfo($cloudUrl, $post_content, $cookie_jar)
    {
        $invokeurl = $cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.QueryGroupInfo.common.kdsvc';
        return $this->invoke_post($invokeurl, $post_content, $cookie_jar, FALSE);
    }

    //保存
    // public function invoke_save($cloudUrl, $post_content, $cookie_jar)
    // {
    //     $invokeurl = $cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc';
    //     return $this->invoke_post($invokeurl, $post_content, $cookie_jar, FALSE);
    // }
    public function invoke_save($formId,$post_content)
        {
            $post_content = $this->create_postdata([$formId,json_encode($post_content)]);
            $invokeurl = $this->cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc';
            return $this->invoke_post($invokeurl, $post_content, $this->KingdeeLogin(), FALSE);
        }

    //审核
    public function invoke_audit($cloudUrl, $post_content, $cookie_jar)
    {
        $invokeurl = $cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc';
        return $this->invoke_post($invokeurl, $post_content, $cookie_jar, FALSE);
    }

    //反审核
    public function invoke_unaudit($formId, $post_content)
    {
        $post_content = $this->create_postdata([$formId,json_encode($post_content)]);
        $invokeurl = $this->cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc';
        return $this->invoke_post($invokeurl, $post_content, $this->KingdeeLogin(), FALSE);
    }

    //删除
    public function invoke_delete($formId, $post_content)
    {
        $post_content = $this->create_postdata([$formId,json_encode($post_content)]);
        $invokeurl = $this->cloudUrl . 'Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc';
        return $this->invoke_post($invokeurl, $post_content, $this->KingdeeLogin(), FALSE);
    }

    //构造Web API请求格式post请求
    public function cookie_jar()
    {
        //定义记录Cloud服务端返回的Session
        @$cookie_jar = tempnam('./tmp', 'CloudSession');
        return @$cookie_jar;
    }


    //构造Web API请求格式post请求
    private function invoke_post($url, $post_content, $cookie_jar, $isLogin)
    {

        $ch = curl_init($url);
        $this_header = array('Content-Type: application/json', 'Content-Length: ' . strlen($post_content));
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($isLogin) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
        } else {
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    //生成guid
    private function create_guid()
    {
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);
        // "-"
        $uuid = chr(123) . substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12) . chr(125);
        // "}"
        return $uuid;
    }


}
